from game import zz_game
from zz import zz_log
import time


class Game(zz_game.ZZ_Game):
    def __init__(self, d):
        super().__init__(d)

    def get_name(self):
        return "福气"

    def do_task_view(self):
        """
        浏览任务
        :return:
        """
        if not self.img_exit("./game/img/福气/task_main.png", 5):
            self.log_msg(f"进入浏览页面失败")
            return

        self.log_msg(f"已进入浏览页面，开始浏览30秒")
        deadline = time.time() + 32
        while True:
            if time.time() > deadline:
                self.log_msg(f"已进入浏览完成")
                break
            self.swipe_up()
            self.d.sleep(1.5)
            self.swipe_up()
            self.d.sleep(1.5)
            self.swipe_down()
            self.d.sleep(1.5)
            self.swipe_down()
            self.d.sleep(1.5)

        self.log_msg("点击返回")
        self.d.press("back")

    def close_alert(self):
        self.log_msg("点击关闭任务列表")
        self.d.click(600, 400)

    def handle_main(self):
        if zz_game.config_get('__task_fu') == 1:
            self.log_msg("福气浏览任务已完成")
            return True

        self.img_click_if_exists("点击玩游戏", "./game/img/play_game00.png", "./game/img/福气/fu_main.png")
        self.img_click_if_exists("点击赚更多", "./game/img/福气/fu_main.png", "./game/img/福气/fu_alert.png")
        self.log_msg("已进入任务列表")
        self.swipe_up()
        self.log_msg("开始获取浏览任务")
        while True:
            task_30s = self.d.xpath('//*[@text="逛30秒可得"]').all()
            if len(task_30s) <= 0:
                self.log_msg("浏览任务-已完成")
                break
            self.log_msg(f"浏览任务总量：{len(task_30s)}")
            for item in task_30s:
                self.log_msg("点击去完成")
                self.d.click(1050, item.center()[1])
                self.d.sleep(2)
                self.do_task_view()
                self.d.sleep(2)
                self.log_msg("点击领取奖励")
                self.xpath_click('//android.widget.TextView[@text="领奖励"]')
                self.log_msg("点击开心收下")
                self.img_click("./game/img/春节/chou_done.png", 5)
                self.log_msg("点击关闭")
                self.xpath_click('//*[@resource-id="com.alipay.mobile.blessingcard:id/cr_close"]')
                self.d.sleep(3)

        self.close_alert()
        zz_game.config_set('__task_fu', 1)
        return True
